angular.module('starter.controllers', [])

.controller('DashCtrl', function($scope) {})

.controller('ChatsCtrl', function($scope, Chats) {
  // With the new view caching in Ionic, Controllers are only called
  // when they are recreated or on app start, instead of every page change.
  // To listen for when this page is active (for example, to refresh data),
  // listen for the $ionicView.enter event:
  //
  //$scope.$on('$ionicView.enter', function(e) {
  //});

  $scope.chats = Chats.all();
  $scope.remove = function(chat) {
    Chats.remove(chat);
  };
})
//注册控制器
  .controller('registerCtrl',function($scope,md5,registerService,$interval){

    $scope.vDisabled=false;
    $scope.vtext="获取验证码";
    $scope.number=60;
    $scope.registerDisabled=true;
    document.getElementById("registerBtn").style.backgroundColor="gray";

    var idInput=document.getElementById("registerName");
    var pwdInput=document.getElementById("registerPwd");
    var againPwdInput=document.getElementById("registerPwdAgain");
    var verificationInput=document.getElementById("vInput");
    var checkBox=document.getElementById("radio");

    //定义input框文本改变时触发的事件
      function changed(){
      if(idInput.value.length==0||pwdInput.value.length==0||againPwdInput.value.length==0||verificationInput.value.length==0||checkBox.checked==false){
        $scope.registerDisabled=true;
        document.getElementById("registerBtn").style.backgroundColor="gray";

      }else{
        $scope.registerDisabled=false;
        document.getElementById("registerBtn").style.backgroundColor="orange";

      }
    }

    $scope.inputChanged=changed;

    var registerUrlParams={
      businessId:REGISTER_APIS.businessId,
      userId:$scope.userId,
      //passwd:md5.create($scope.passwd),
      passwd:$scope.passwd,
      registrationId:REGISTER_APIS.registrationId,
      registerType:REGISTER_APIS.registerType,
      verificationCode:'1'
    };
    //注册按钮点击事件
    $scope.register=function(){
      registerService.getData(registerUrlParams).then(function(data){
      })
    };
    //设置验证码按钮点击事件
    $scope.verificationClicked=function(){

      var time=$interval(function(){
        //设置按钮状态为不可点击
        $scope.vDisabled=true;
        document.getElementById("vBtn").style.backgroundColor="gray";

        $scope.number--;
        console.log($scope.number);
        //通过数据的双向绑定，改变按钮的文本值
        $scope.vtext=$scope.number+"秒后重发";
        if($scope.number==0){
          $interval.cancel(time);
          $scope.vtext="获取验证码";
          $scope.number=60;
          $scope.vDisabled=false;
          document.getElementById("vBtn").style.backgroundColor="dodgerblue";

        }
      },1000);
    }
    //服务条款选择框
    $scope.radioClicked=function(){
      var radioBtn=document.getElementById("radio");
      changed();
      if($scope.radioState==false){
        $scope.radioState=true;
        console.log("111");
      }
      else{
        $scope.radioState=true;
        console.log("222");
      }
    }

  })
  //登录控制器
  .controller('loginCtrl',function($scope,loginService){

    var loginUrlParams={
      loginName:$scope.loginName,
      loginPwd:$scope.loginPwd,
      registrationId:LOGIN_APIS.registrationId,
      registerType:LOGIN_APIS.registerType,
      isComeFromContract:LOGIN_APIS.isComeFromContract
    };
    $scope.loginDisabled=true;
    var loginBtn=document.getElementById("login");
    loginBtn.style.backgroundColor="gray";

    var nameInput=document.getElementById("loginName");
    var pwdInput=document.getElementById("loginPwd");

    function changed(){
      if(nameInput.value.length!=0&&pwdInput.value.length!=0){
        $scope.loginDisabled=false;
        loginBtn.style.backgroundColor="orange";

      }else{
        $scope.loginDisabled=true;
        loginBtn.style.backgroundColor="gray";
      }
    }
    $scope.inputChanged=changed;

    $scope.signIn=function(){

      loginService.getData(loginUrlParams).then(function(data){
        alert("login");
        console.log(data);
        console.log(data.rs);
      })

    };
  })

.controller('ChatDetailCtrl', function($scope, $stateParams, Chats) {
  $scope.chat = Chats.get($stateParams.chatId);
})

.controller('AccountCtrl', function($scope,registerService) {

    $scope.data={};

    var urlParams={
      businessId:'1',
      userId:'user',
      passwd:'123',
      verificationCode:'1',
      registrationId:'1',
      registerType:'1'
    }

    $scope.register=function(){

        registerService.getData(urlParams).then(function(data){
          console.log(data);
          console.log(data.resultCode);
        })

    }


/*
    $scope.register=function(){
      registerService.getData(urlParams).then(function(data){
        alert(data);
      })
    };
    */
  $scope.settings = {
    enableFriends: true
  };
});
